import { defineStore } from 'pinia';
import { Moddle } from 'moddle';
import { Base } from 'diagram-js/lib/model';
import Modeler from 'bpmn-js/lib/Modeler';
import Modeling from 'bpmn-js/lib/features/modeling/Modeling.js';
import Canvas from 'diagram-js/lib/core/Canvas';
import ElementRegistry from 'diagram-js/lib/core/ElementRegistry';
import { cloneDeep } from 'lodash-es';

type ModelerStore = {
  activeElement: Base | undefined;
  activeElementId: string | undefined;
  modeler: Modeler | null;
  moddle: Moddle | null;
  modeling: Modeling | null;
  canvas: Canvas | null;
  elementRegistry: ElementRegistry | null;
  activities: any;
  baseInfo: any;
  flowInfo: any;
  configuration: any;
  nodeItem: any;
  fieldFormList: any;
};

const defaultState: ModelerStore = {
  activeElement: undefined,
  activeElementId: undefined,
  modeler: null,
  moddle: null,
  modeling: null,
  canvas: null,
  elementRegistry: null,
  activities: [],
  baseInfo: {},
  flowInfo: {},
  configuration: {
    menus: [],
    roles: [],
    props: [],
    rights: [],
  },
  nodeItem: null,
  fieldFormList: [],
};

export default defineStore('modeler', {
  state: () => defaultState,
  getters: {
    getActive: (state) => state.activeElement,
    getActiveId: (state) => state.activeElementId,
    getModeler: (state) => state.modeler,
    getModdle: (state) => state.moddle,
    getModeling: (state) => state.modeling,
    getCanvas: (state) => state.canvas,
    getElRegistry: (state) => state.elementRegistry,
    getActivities: (state) => state.activities,
    getBaseInfo: (state) => state.baseInfo,
    getFlowInfo: (state) => state.flowInfo,
    getConfiguration: (state) => state.configuration,
    getNodeItem: (state) => state.nodeItem,
    getFieldFormList: (state) => state.fieldFormList,
  },
  actions: {
    setModeler(modeler) {
      this.modeler = modeler;
    },
    setModules<K extends keyof ModelerStore>(key: K, module) {
      this[key] = module;
    },
    setElement(element: Base, id: string) {
      this.activeElement = element;
      this.activeElementId = id;
    },
    // 流程设置
    setActivities(activities) {
      setTimeout(() => {
        this.activities = activities;
      });
    },
    setBaseInfo(baseInfo) {
      this.baseInfo = baseInfo;
    },
    setFlowInfo(flowInfo) {
      this.flowInfo = flowInfo;
    },
    setConfiguration(configuration) {
      this.configuration = configuration;
    },
    setActivitiesItem(id, type, value) {
      this.activities.forEach((item) => {
        if (item.bpmnId === id) {
          if (type === 'opinion') {
            item.baseInfo.responseChoices = value;
          } else if (type.indexOf('.') != -1) {
            const fields = type.split('.');
            item[fields[0]][fields[1]] = value;
          } else {
            item[type] = value;
          }
        }
      });
    },
    // 添加元素
    addElement(bpmnElement: Object) {
      const judeg = function (type) {
        let ret: any = 0;
        switch (type) {
          case 1: //任务类型
            ret = 1;
            break;
          case 2: //通知类型
            ret = 0;
            break;
          default:
            ret = null;
        }
        return ret;
      };
      const start = cloneDeep(this.activities?.[0] ?? {});

      const param: any = {
        ...start,
        ...bpmnElement,
        U0: [],
        R0: [],
        U1: [],

        remindAddFlag: [],
        rangeType: start?.participant?.rangeType,
        // rangeComponentType: start?.participant?.rangeComponentType,
        selectUserNum: start?.participant?.selectUserNum,
        maxLimitedNum: start?.participant?.maxLimitedNum,
        caculateReturnFlag: start?.participant?.caculateReturnFlag,
        caculateFormula: start?.participant?.caculateFormula,
        participantRange: start.participant?.participantRange,

        transLimit: start?.handleContent?.transLimit,
        compareCont: start?.handleContent?.compareCont,
        limitCompare: start?.handleContent?.limitCompare,
        transLimitChoice: start?.handleContent?.transLimitChoice,
        baseInfo: {
          rights: [2],
          responseChoices: '同意;拒绝;',
          sendLabel: '',
          save1Label: '',
          save2Label: '',
          sendStyle: '送{[<某某某>]}{[<步骤名称>]}',
        },
        rights: [1],
        fieldPerms: [],
        notifyInitiatorFlag: judeg((bpmnElement as any).activityType),
      };

      this.activities.push(param);
    },
    setNodeItem(nodeItem) {
      this.nodeItem = nodeItem;
    },
    setFieldFormList(fieldFormList) {
      this.fieldFormList = fieldFormList;
    },
    setFieldPerms() {
      this.activities.forEach((item) => {
        item.fieldPerms = [];
      });
    },
  },
});
